package cn.iocoder.yudao.module.member.agency.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.CrudService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.agency.dal.dataobject.AgencyApplyDO;
import cn.iocoder.yudao.module.member.agency.dal.dataobject.AgencyPackageDO;
import cn.iocoder.yudao.module.member.agency.dal.mysql.AgencyApplyMapper;
import cn.iocoder.yudao.module.member.agency.dal.mysql.AgencyPackageMapper;
import cn.iocoder.yudao.module.member.agency.vo.AgencyPackageQueryDTO;
import cn.iocoder.yudao.module.member.agency.vo.AgencyPackageSaveDTO;
import cn.iocoder.yudao.module.member.agency.vo.AgencyPackageVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 招商人员缴费套餐 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class AgencyPackageServiceImpl extends BaseService<AgencyPackageDO, AgencyPackageVO, AgencyPackageQueryDTO> implements AgencyPackageService, CrudService<AgencyPackageDO, AgencyPackageVO, AgencyPackageSaveDTO, AgencyPackageQueryDTO> {
    @Resource
    private AgencyPackageMapper agencyPackageMapper;
    @Resource
    private AgencyApplyMapper agencyApplyMapper;

    @Override
    public AgencyPackageVO getOne(Long userId) {
        CoreQueryWrapper<AgencyPackageDO> wrapper = buildQueryWrapper(userId);
        wrapper.limitN(1);
        AgencyPackageDO aDO = getInjectedMapper().selectOne(wrapper);
        return BeanUtils.toBean(aDO, AgencyPackageVO.class);
    }

    @Override
    public List<AgencyPackageVO> getList(Long userId, @Nullable Integer limit) {
        CoreQueryWrapper<AgencyPackageDO> wrapper = buildQueryWrapper(userId);
        List<AgencyPackageDO> list = getInjectedMapper().selectList(wrapper);
        return BeanUtils.toBean(list, AgencyPackageVO.class);
    }

    @Nonnull
    private CoreQueryWrapper<AgencyPackageDO> buildQueryWrapper(Long userId) {
        CoreQueryWrapper<AgencyPackageDO> wrapper = new CoreQueryWrapper<>();
        if (userId != null && userId > 0) {
            AgencyApplyDO applyDO = agencyApplyMapper.selectOne(AgencyApplyDO::getUserId, userId);
            if (applyDO != null) {
                wrapper.eq("level", applyDO.getLevel());
            }
        }
        wrapper.orderByAsc("sort");
        wrapper.orderByAsc("price");
        wrapper.orderByDesc("id");
        return wrapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<AgencyPackageDO> buildQueryWrapper(AgencyPackageQueryDTO param) {
        CoreQueryWrapper<AgencyPackageDO> wrapper = super.buildQueryWrapper(param);
        wrapper.likeIfPresent("name", param.getName());
        wrapper.eqIfPresent("level", param.getLevel());
        return wrapper;
    }

    @Override
    public AgencyPackageMapper getInjectedMapper() {
        return agencyPackageMapper;
    }

    @Override
    public AgencyPackageDO convertToEntity(AgencyPackageSaveDTO param) {
        return BeanUtils.toBean(param, AgencyPackageDO.class);
    }

}